package com.cgnpc.scp.adapter.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.controller.BaseController;
import com.cgnpc.cud.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);

    /**********************************
     * 用途说明: 测试分页
     * 参数说明
     * 返回值说明:
     ***********************************/
    @GetMapping("testAdapter")
    @UBA(module = "接口机模块", action = "获取当前用户", channel = OperatorType.Page)
    public AjaxResult getCurrentUser() {
        try {
            String returnInfo = "已到达后台系统";

//            // begin 分页查询
//            int pageNum = 2; // 页码
//            int pageSize = 10; // 每页显示数量
//            // 创建Page对象，将pageNum，pageSize参数传入，必须位于从数据库查询数据的语句之前，否则不生效
//            Page<DataDict> queryPage = PageHelper.startPage(pageNum, pageSize);
//
//            QueryWrapper<DataDict> queryWrapper = new QueryWrapper<>();
//            QueryWrapper<DataDict> order_index = queryWrapper.orderByAsc("lang desc");
//            queryWrapper.eq("lang", "zh_CN");
//            // List<DataDict> dbDataDictList = dataDictMapper.selectList(order_index);
//            dataDictMapper.selectList(order_index);
//
//            PageInfo<DataDict> pageResult = new PageInfo<>(queryPage.getResult());
//            long pageResultTotal = pageResult.getTotal(); // 返回总数
//            List<DataDict> pageResultList = pageResult.getList(); // 返回当前的列表,pageInfoDataList=dbDataDictList
//            // end 分页查询

            return success(returnInfo);
        } catch (Exception e) {
            LOGGER.error("error;", e);
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 测试接口机转发
     * 参数说明
     * 返回值说明:
     ***********************************/
    @GetMapping("testPortal")
    @UBA(module = "测试模块", action = "测试转发")
    public AjaxResult testAgent(HttpServletRequest request) {
        String strings = extractCookieList(request);
        String returnInfo = "已收到接口机转发并返回：i m portal=====" + strings;
        return success(returnInfo);
    }

    public static String extractCookieList(HttpServletRequest request) {
        String requestString = null;

        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length == 0) {
            return null;
        }

        for (Cookie cookie : cookies) {
            requestString = requestString + cookie.getName() + "=" + cookie.getValue() + ";";
        }

        return requestString;
    }


}
